<?php

class Role_PermitController extends Zend_Controller_Action {
	private $menu;
	private $privilege;
	
	public function indexAction(){
		$businessMenu = new Menu_Models_Business_Menu();
		$data = $businessMenu->getMenu();
		
		foreach ($data as $key=>$value) {
			$parent = $value["parent"];
			$this->menu[$parent][]=$value;
		}
		
		$businessRole = new Role_Models_Business_Role();
		$jsonObject = $businessRole->getRolePrivilege();
		$data = Zend_Json::decode($jsonObject, Zend_Json::TYPE_ARRAY);
		$data = $data["data"];
		
		foreach ($data as $key=>$value) {
			$menu_id = $value["id"];
			$this->privilege[$menu_id][]=$value;
		}
		
		foreach ($this->menu[0] as $key=>$value){
			$objTree = new stdClass;
			$objTree->id = "menu_".$value["id"];
			$objTree->text = html_entity_decode($value["display_name"]);
			///$objTree->cls = 'folder';
			
			//$obj->leaf = false;			
			//$obj->checked = false;
			
			$objTree->singleClickExpand = true;			
			$objTree->children=$this->subMenu($value["id"]);
			$tree[]= $objTree;
		}
		echo json_encode($tree);
		$this->_helper->layout->disableLayout();
	}	
	
	private function subMenu($id)
	{
		if (@$this->menu[$id])
		{
			$children = array();
			foreach ($this->menu[$id] as $key=>$value){
				$obj = new stdClass;
				$obj->text = html_entity_decode($value["display_name"]);				
				$obj->id = "menu_".$value["id"];
				$obj->leaf = false;
				//$obj->checked = false;
				$obj->children=$this->subMenu($value["id"]);				
				$children[] = $obj;
			}			
		}
		else
		{
			if(@$this->privilege[$id])
			{
				$children = array();
				foreach ($this->privilege[$id] as $key=>$value){
					$obj = new stdClass;
					$obj->text = html_entity_decode($value["display_name"]);					
					$obj->id = $value["mp_id"];
					$obj->leaf = true;
					$obj->checked = false;	
					$children[] = $obj;				
				}
			}
		}
		return $children;
	} 
}


?>